<?php
require_once('/config/constants.php');
require_once(realpath(DOCUMENT_ROOT) . PROJECT_BASE .  MAPPATH . 'Map.php');
require_once(realpath(DOCUMENT_ROOT) . PROJECT_BASE .  UTILPATH . 'Util.php');

if(isset($_REQUEST['LOAD'])) {
    $map = new Map(Util::generateRandomNumber(1, 20), Util::generateRandomNumber(1, 20));
    $map->loadMapFromFile("map");
} elseif(isset($_REQUEST['SAVE'])) {
    $map = new Map(Util::generateRandomNumber(1, 20), Util::generateRandomNumber(1, 20));
    $map->saveMapToFile("map");
    $map->loadMapFromFile("map");
} else {
    $map = new Map(Util::generateRandomNumber(1, 20), Util::generateRandomNumber(1, 20));
}


echo "
<html>
<head>
<title>" . PROEJCT_TITLE . "</title>
<script src='" . PROJECT_BASE . JSPATH . "/jquery.js' type='text/javascript'></script>
<script src='" . PROJECT_BASE . JSPATH . "/javascript.js' type='text/javascript'></script>
</head>

<form method=\"POST\" action=\"index.php\">
<table>";

foreach($map->getTileSet() AS $tileRow) {
    echo "<tr>";
    foreach($tileRow AS $tile) {
        echo "<td>
        		<img
        			width='" . $tile->getTileWidth() ."'
        			height='" . $tile->getTileHeight() ."'
        			src='" . PROJECT_BASE . IMAGEPATH . $tile->getTileImageName() ."'/></td>";
    }
    echo "</tr>";
}

echo "
</table>
<p><input type=\"submit\" name=\"LOAD\" value=\"LOAD MAP\" /></p>
<span name=\"SAVE\" value=\"SAVE MAP\" onClick='save();'>SAVE MAP</span>
</form>
</html>";